<?php

global $_GPC;
global $_W;
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
$shopid = check_right(3);
if (!$shopid) {
    message('请先选择需要管理的店铺', $this->createWebUrl('shop'), 'error');
}
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=\'' . $shopid . '\'');
if ($operation == 'display') {
    $where .= $_GPC['groupid'] ? ' and pcate=\'' . $_GPC['groupid'] . '\' ' : ' and pcate=\'' . $shopid . '\' ';
    if ($_GPC['keyword']) {
        $where .= ' and (useracount like \'%' . $_GPC['keyword'] . '%\' or realname like \'%' . $_GPC['keyword'] . '%\' or mobile like \'%' . $_GPC['keyword'] . '%\' )';
    }
    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_user') . ' WHERE weid = \'' . $_W['uniacid'] . '\' ' . $where . ' order by status desc, id desc');
    $list2 = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_staffcate') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=:a order by id asc', array(':a' => $shopid));
    $staffary = array();
    foreach ($list2 as $row) {
        $staffary[$row['id']] = $row['title'];
    }
    $workstatus = array(0 => '试用', 1 => '正式', 2 => '学徒', 3 => '实习', 4 => '休假', 5 => '离职');
} else {
    if ($operation == 'staffcate') {
        $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_staffcate') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=:a order by id asc', array(':a' => $shopid));
    } else {
        if ($operation == 'getstaffcate') {
            $id = $_GPC['id'];
            $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_staffcate') . ' WHERE id = :id ', array(':id' => $id));
            exit(json_encode(array('success' => true, 'item' => $item)));
        } else {
            if ($operation == 'staffcatepost') {
                $id = intval($_GPC['id']);
                $data = array('weid' => $_W['uniacid'], 'title' => $_GPC['title'], 'groupid' => intval($shopid));
                //if($id=="0"){$id="";}
                if (!empty($id)) {
                    pdo_update('j_hmoney_staffcate', $data, array('id' => $id));
                }else {
                    pdo_insert('j_hmoney_staffcate', $data);
                    $id = pdo_insertid();
                }
                exit(json_encode(array('success' => true)));
            } else {
                if ($operation == 'post') {
                    $id = $_GPC['id'];
                    if ($id) {
                        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_user') . ' WHERE id = :id ', array(':id' => $id));
                    } else {
                        if ($cfg['usernum']) {
                            $allgroup = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_user') . ' WHERE weid=\'' . $_W['uniacid'] . '\'');
                            if ($allgroup >= $cfg['usernum']) {
                                message('只能添加' . $cfg['usernum'] . '个坐席，增加请联系服务商');
                            }
                        }
                    }
                    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_staffcate') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=:a order by id asc', array(':a' => $shopid));
                    $users = pdo_fetchall('SELECT * FROM ' . tablename('users') . ' where uid in (SELECT uid FROM ' . tablename('users_permission') . ' WHERE uniacid = \'' . $_W['uniacid'] . '\' group by uid )');
                    $grouplist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' ORDER BY id asc');
                    $groupary = array();
                    foreach ($grouplist as $row) {
                        $groupary[$row['id']] = $row['companyname'];
                    }
                    if (checksubmit('submit')) {
                        $data = array('useracount' => $_GPC['useracount'], 'weid' => $_W['uniacid'], 'headimg' => $_GPC['headimg'], 'realname' => $_GPC['realname'], 'openid' => $_GPC['openid'], 'mobile' => $_GPC['mobile'], 'password' => $_GPC['password'] ? md5($_GPC['password']) : '', 'status' => intval($_GPC['status']), 'pcate' => intval($_GPC['pcate']), 'permission' => intval($_GPC['permission']), 'staffcate' => intval($_GPC['staffcate']), 'idcard' => $_GPC['idcard'], 'birday' => $_GPC['birday'], 'entrytime' => $_GPC['entrytime'], 'contracttime' => $_GPC['contracttime'], 'basesalary' => intval($_GPC['basesalary'] * 100), 'bankname' => $_GPC['bankname'], 'bankcard' => $_GPC['bankcard'], 'workstatus' => $_GPC['workstatus']);
                        if (!$data['pcate']) {
                            message('请选择所属组别');
                        }
                        if (!$data['staffcate']) {
                            message('请选择工种');
                        }
                        if ($id) {
                            if (!$data['password']) {
                                unset($data['password']);
                            }
                            unset($data['useracount']);
                            pdo_update('j_hmoney_user', $data, array('id' => $id));
                        } else {
                            if ($cfg['usernum']) {
                                $allgroup = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_user') . ' WHERE weid=\'' . $_W['uniacid'] . '\'');
                                if ($allgroup >= $cfg['usernum']) {
                                    message('只能添加' . $cfg['usernum'] . '个坐席，增加请联系服务商');
                                }
                            }
                            $isUsed = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_user') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and useracount=:a', array(':a' => $data['useracount']));
                            if ($isUsed) {
                                message('【' . $data['useracount'] . '】已经被使用，请更换其他工号');
                            }
                            $data['createtime'] = NULL;
                            pdo_insert('j_hmoney_user', $data);
                            $id = pdo_insertid();
                        }
                        message('修改完成', $this->createWebUrl('staff', array('op' => 'post', 'id' => $id)), 'success');
                    }
                } else {
                    if ($operation == 'delete') {
                        $id = intval($_GPC['id']);
                        if ($id) {
                            pdo_delete('j_hmoney_user', array('id' => $id));
                        }
                        message('删除成功', $this->createWebUrl('staff'), 'success');
                    } else {
                        if ($operation == 'changepass') {
                            $id = intval($_GPC['id']);
                            $pass = trim($_GPC['pass']);
                            if (strlen($pass) < 6) {
                                exit(json_encode(array('success' => false, 'msg' => '密码长度错误')));
                            }
                            $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_user') . ' WHERE id = :id ', array(':id' => $id));
                            if (!$item) {
                                exit(json_encode(array('success' => false, 'msg' => '用户不存在')));
                            }
                            $data = array('password' => md5($pass));
                            pdo_update('j_hmoney_user', $data, array('id' => $id));
                            exit(json_encode(array('success' => true)));
                        }
                    }
                }
            }
        }
    }
}
include $this->template('web/' . $cfg['floder'] . '/staff');
//$data['createtime'] = NULL;
//pdo_insert('j_hmoney_user', $data);
//$id = pdo_insertid();